package io.zhengsh.juc._0thread.pool;

import java.util.concurrent.*;

public class ThreadPoolTest {

    public static void main(String[] args) {
        test();
//        ExecutorService executor = Executors.newFixedThreadPool(5);
//
//        for (int i = 0; i < 100; i++) {
//            executor.execute(new Task("Task-" + i));
//        }
    }

    private static void test() {
        ExecutorService executorService1 =
                new ThreadPoolExecutor(2, 4, 2,
                        TimeUnit.SECONDS, new LinkedBlockingQueue<>(200), new ThreadFactory() {
                    int i = 0;

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, String.format("test-thread-%d", i++));
                    }
                }, new ThreadPoolExecutor.CallerRunsPolicy());

        System.out.println(executorService1);

        //Executors.newCachedThreadPool();
        //ExecutorService executorService2 = Executors.newFixedThreadPool(10);
        //ExecutorService executorService3 = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 100; i++) {
            int idx = i;
            executorService1.submit(() ->
                    System.out.println(Thread.currentThread().getName() + " task execution i = " + idx));
        }
    }
}
